package de.iplon.DataViewer;

import java.util.Date;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;

public class Diagram extends Activity 
{
	private GraphicalView mChartView;
	private XYMultipleSeriesDataset mDataset;
	private XYMultipleSeriesRenderer mRenderer;
	private String mSource;
	private HttpSerie mChannel1;
	private FrameLayout mLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_diagram);
		// Show the Up button in the action bar.
		setupActionBar();
	
		//new Channel Energy Meter
		mChannel1 = new HttpSerie("http://arun.pv-india.net/AnjarApi/energy_graph", "Energy Meter");
		
		//http Request
		mChannel1.start();
		while (!mChannel1.isReady())
		{
			try 
			{
				Thread.sleep(1000);
			}
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
		
		if(!mChannel1.getError())
		{		
			//create Dataset
			this.createDataset();
		
			//Setup-Renderer
			this.setup_Renderer();
			
			//Setup-Chart
			this.setup_Chart();
			
			mLayout = (FrameLayout) findViewById(R.id.layout1);
			mLayout.addView(mChartView);
		}
		else
		{
			//Error Message
			Toast.makeText(this, "HttpRequest Error",Toast.LENGTH_LONG).show();
		}	
	}

	//Setup-Renderer
	private void setup_Renderer()
	{
		mRenderer = new XYMultipleSeriesRenderer();
		mRenderer.setChartTitle("Energy Meter");
		mRenderer.setChartTitleTextSize(30);
	    mRenderer.setXTitle("Time");
	    mRenderer.setYTitle("kW");
	    mRenderer.setMargins(new int[] {120, 15, 30, 0});
	    mRenderer.setYAxisMax(6000.0);
	    mRenderer.setYLabels(10);
	    mRenderer.setXLabels(17);
	    mRenderer.setLabelsTextSize((float)(15.0));
	    mRenderer.setAxisTitleTextSize((float)20.0);
	    mRenderer.setShowGrid(true);
	    
	    XYSeriesRenderer r = new XYSeriesRenderer();
	    r.setColor(Color.GREEN);
	    r.setLineWidth((float)4.0 );
	    r.setFillPoints(true);
	    r.setFillBelowLine(true);
	    r.setFillBelowLineColor(Color.argb(0x33, 0, 0xFF, 0));
	    
	    Date temp_Date = new Date();
	    temp_Date = mChannel1.getDate();
	    temp_Date.setHours(6);
	    temp_Date.setMinutes(0);
	    temp_Date.setSeconds(0);
	    mRenderer.setXAxisMin(temp_Date.getTime());
	    double[] zoomlimit = new double[4];
	    zoomlimit[0] = temp_Date.getTime();
	    temp_Date.setHours(21);
	    temp_Date.setMinutes(0);
	    temp_Date.setSeconds(0);
	    mRenderer.setXAxisMax(temp_Date.getTime());
	    zoomlimit[1] = temp_Date.getTime();
	    zoomlimit[2] = 0.0;
	    zoomlimit[3] = 6000.0;
	    mRenderer.setZoomLimits(zoomlimit);
	    mRenderer.setPanEnabled(false, false);
	    mRenderer.addSeriesRenderer(r);
	}
	
	//Setup-Chart
	private void setup_Chart()
	{
		mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer, "H:mm");
	}

	//create Dataset
	void createDataset() 
	{	
		mDataset = new XYMultipleSeriesDataset();
		//Add Channel1
		mDataset.addSeries(mChannel1.getSerie());
	}
	
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() 
	{
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
		{
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.diagram, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		switch (item.getItemId()) 
		{
			case android.R.id.home:
				// This ID represents the Home or Up button. In the case of this
				// activity, the Up button is shown. Use NavUtils to allow users
				// to navigate up one level in the application structure. For
				// more details, see the Navigation pattern on Android Design:
				//
				// http://developer.android.com/design/patterns/navigation.html#up-vs-back
				//
				NavUtils.navigateUpFromSameTask(this);
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
